//
//  EditPersonalDataViewController.m
//  安好
//
//  Created by 马晓良 on 14-11-7.
//  Copyright (c) 2014年 lianchuangbrother. All rights reserved.
//

#import "EditPersonalDataViewController.h"
#import "EditPersonalDataCell.h"
#import "EditSignatureViewController.h"
#import "ModifyPersonInfoViewController.h"
#import "HYDatePicker.h"
#import "PersonalMobileViewController.h"
#import "HZAreaPickerView.h"
@interface EditPersonalDataViewController ()<UITableViewDataSource,UITableViewDelegate,UIActionSheetDelegate,UIAlertViewDelegate,UIAlertViewDelegate,HZAreaPickerDelegate>
{
    BOOL change;
    
}
/**
 * 数据的显示
 */
@property (strong, nonatomic) IBOutlet UITableView *editPersonalDataTableView;
/**
 *  用来记录点击的cell对象
 */
@property (strong, nonatomic) EditPersonalDataCell *clickCell;
/**
 *  两个数组分别存放属性名称和跳转页面的placeholder
 */
@property (nonatomic, strong) NSArray *nameTypeArray;
@property (nonatomic, strong) NSArray *nextViewPlaceholder;

/**
 *  用来呈现pickerView
 */
@property (nonatomic, strong) UIView *xinsView;
/**
 *  日期显示
 */
@property (nonatomic, strong) HYDatePicker *datePicker;
/**
 *  地区显示
 */
@property (nonatomic, strong) HZAreaPickerView *locatePicker;
/**
 *  获取点击的UIActionSheet点击的位置
 */
@property (nonatomic, assign) NSInteger actionSheetButtonIndex;

/**
 *  要上传的数据存在此字典中
 */
@property (nonatomic, strong) NSMutableDictionary *uploadInfoDic;
/**
 *  存放类型的字典
 */
@property (nonatomic, strong) NSDictionary *typeDic;
/**
 *  存储到本地
 */
@property (nonatomic, strong) NSUserDefaults *settings;


@property (nonatomic, strong) NSString *areaString;

@property (nonatomic, strong) NSString *proviceId;
@property (nonatomic, strong) NSString *cityId;



/**
 *  日期选择器显示
 */
@property(nonatomic,strong)MyDatePickerView * myDatePickerView;
@end

@implementation EditPersonalDataViewController
- (NSMutableDictionary *)uploadInfoDic{
    if (_uploadInfoDic == nil) {
        self.uploadInfoDic = [NSMutableDictionary dictionary];
    }
    return _uploadInfoDic;
}


- (NSDictionary *)typeDic{
    if (_typeDic == nil) {
        self.typeDic = [NSDictionary dictionary];
    }
    return _typeDic;
}

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.showPickerView.hidden = YES;
    // Do any additional setup after loading the view from its nib.
    change = YES;
    [self setNaviBarAttribute];
    
    [self makeBirthdayPickerView];
    [self addLocatePicker];
    
    self.settings = [NSUserDefaults standardUserDefaults];
    if ([self.userDetailObject.gender isEqualToString: @"0"]) {
        [self.settings setBool:NO forKey:@"Bool"];;
    }
    sexBool = [self.settings boolForKey:@"Bool"];
    
    self.navigationItem.title = @"个人资料";
    self.editPersonalDataTableView.backgroundColor = [ClassMethodsViewController colorWithHexString:BACK_BACKGROUNDCOLOR_16];
    self.editPersonalDataTableView.dataSource = self;
    self.editPersonalDataTableView.delegate = self;
    // 注册cell
//    [self.editPersonalDataTableView registerNib:[UINib nibWithNibName:@"EditPersonalDataCell" bundle:nil ]forCellReuseIdentifier:@"editPersonalDataCell"];
    self.nameTypeArray = @[@"昵称",@"真实姓名",@"身份证",@"个人邮箱",@"手机号码"];
    self.nextViewPlaceholder = @[@"请输入您的昵称",@"请输入您的真实姓名",@"请输入您的身份证号",@"请输入您的个人邮箱",@"请输入您的手机号"];
    // 存放次字典是为了获取上传时字典的key;
    self.typeDic = @{@"个性签名":@"bio",@"性      别":@"gender",@"出生年月":@"birthday",@"地      区":@"address"};
    
    MyNSLog("=======%@", self.userDetailObject.idcard);
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
//    [self _parsePersonalDataSource:kBaseURL];
    [self refreshData:kBaseURL];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 设置naviBar 并实现左右naviBar button方法 - 
- (void)setNaviBarAttribute{
    /**
     *  设置左NaviBar和右naviBar
     */
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.frame = CGRectMake(0, 0, 23, 23);
    [leftButton setImage:[UIImage imageNamed:@"公用_返回"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(handleLeftButtonAction) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
    
}

/**
 *  返回
 */
- (void)handleLeftButtonAction{
    if (_delegate && [_delegate respondsToSelector:@selector(controlRefresh)]) {
        [_delegate controlRefresh];
    }
    [self.navigationController popViewControllerAnimated:YES];
}


#pragma mark - tableView的代理方法 -

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 3;
    }else if (section == 1){
        return 5;
    }
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (kScreenWidth == 414) {
        return 54;
    }else{
        return 44;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.1;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headerView = [[UIView alloc] initWithFrame:self.view.bounds];
    headerView.backgroundColor = [ClassMethodsViewController colorWithHexString:@"f2f2f2"];
    return headerView;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *indentify = @"editPersonalDataCell";
    EditPersonalDataCell *cell = [tableView dequeueReusableCellWithIdentifier:indentify];
    if (!cell) {
        if (kScreenWidth == 414) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"EditPersonalDataCell" owner:self options:nil] lastObject];
        }else{
            cell = [[[NSBundle mainBundle] loadNibNamed:@"EditPersonalDataCell" owner:self options:nil] firstObject];
        }
    }
    if(indexPath.section == 0){
        if (indexPath.row == 0) {
            cell.typeName.text = @"昵      称";
            cell.typeContent.hidden = YES;
            cell.typeContentHiden.hidden = NO;
            cell.typeContentHiden.text = self.userDetailObject.username;
            cell.jumpImageView.hidden = YES;
        }else if (indexPath.row == 1){
            cell.typeName.text = @"真实姓名";
            if (self.userDetailObject.realname.length > 0) {
                cell.typeContent.hidden = YES;
                cell.typeContentHiden.hidden = NO;
                cell.typeContentHiden.text = self.userDetailObject.realname;
                cell.jumpImageView.hidden = YES;
            }else{
                cell.typeContent.text = self.userDetailObject.realname;
            }
        }else if (indexPath.row == 2){
            cell.typeName.text = @"个性签名";
            cell.typeContent.text = self.userDetailObject.bio;
        }
    }else if (indexPath.section == 1){
        if (indexPath.row == 0) {
             cell.typeName.text = @"性      别";
            if ([self.userDetailObject.gender isEqualToString:@"0"]) {
                cell.typeContent.text = @"";
            }else if ([self.userDetailObject.gender isEqualToString:@"1"]){
                cell.typeContentHiden.text = @"男";
                cell.typeContent.hidden = YES;
                cell.typeContentHiden.hidden = NO;
                cell.jumpImageView.hidden = YES;
            }else if ([self.userDetailObject.gender isEqualToString:@"2"]){
                cell.typeContentHiden.text = @"女";
                cell.typeContent.hidden = YES;
                cell.typeContentHiden.hidden = NO;
                cell.jumpImageView.hidden = YES;
            }else{
                cell.typeContent.text = @"";
            }
        }else if (indexPath.row == 1){
            cell.typeName.text = @"出生年月";
            if ([self.userDetailObject.birthday isEqualToString:@"0"]) {
                cell.typeContent.text = @"";
            }else{
                 cell.typeContent.text = self.userDetailObject.birthday;
            }
        }else if (indexPath.row == 2){
            cell.typeName.text = @"地      区";
            cell.typeContent.text = self.userDetailObject.address;
        }else if (indexPath.row == 3){
            cell.typeName.text = @"身 份 证";
            if (self.userDetailObject.idcard.length > 0) {
                cell.typeContent.hidden = YES;
                cell.typeContentHiden.hidden = NO;
                cell.typeContentHiden.text = self.userDetailObject.idcard;
                cell.jumpImageView.hidden = YES;
            }else{
                cell.typeContent.text = self.userDetailObject.idcard;
            }
//            cell.typeContent.text = self.userDetailObject.idcard;
        }else if (indexPath.row == 4){
            cell.typeName.text = @"个人邮箱";
            if (self.userDetailObject.email.length > 1) {
                cell.typeContent.hidden = YES;
                cell.typeContentHiden.hidden = NO;
                cell.typeContentHiden.text = self.userDetailObject.email;
                cell.jumpImageView.hidden = YES;
            }else{
                 cell.typeContent.text = self.userDetailObject.email;
            }
//            cell.typeName.text = @"个人邮箱";
//            cell.typeContentHiden.text = self.userDetailObject.email;
//            cell.typeContentHiden.hidden = NO;
//            cell.typeContent.hidden = YES;
//            cell.jumpImageView.hidden = YES;
        }
//        else if (indexPath.row == 5){
//            cell.typeName.text = @"手机号码";
//            cell.typeContent.text = self.userDetailObject.mobile;
//        }
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    // 获取当前点击的cell
    self.clickCell = (EditPersonalDataCell *)[self.editPersonalDataTableView cellForRowAtIndexPath:indexPath];
    /**
     *  在点击cell是判断pickerView是否存在 存在了remove
     */
    [UIView animateWithDuration:0.35 animations:^{
        self.myDatePickerView.frame = CGRectMake(0, kScreenHeight-iOS7TopMargin, kScreenWidth, 200);
    } completion:^(BOOL finished) {}];
    [UIView animateWithDuration:0.35 animations:^{
        self.locatePicker.frame = CGRectMake(0, kScreenHeight-iOS7TopMargin, kScreenWidth, 200);
    } completion:^(BOOL finished) {}];
    
    if (0 == indexPath.section) {
        if (0 == indexPath.row) {
//            // 昵称
//            [self didSelectTableViewRowJumpAction:self.nameTypeArray[0] placeholderStr:self.nextViewPlaceholder[0]];
        }else if (1 == indexPath.row){
            // 真实姓名
            if (self.userDetailObject.realname.length > 0) {
                
            }else{
                 [self didSelectTableViewRowJumpAction:self.nameTypeArray[1] placeholderStr:self.nextViewPlaceholder[1]];
            }
        }else if (2 == indexPath.row) {
            // 签名
            EditSignatureViewController *signatureVC = [[EditSignatureViewController alloc] init];
            [signatureVC getTextViewInfo:^(NSString *textViewText) {
                self.clickCell.typeContent.text = textViewText;
            // 存入字典中
            [self.uploadInfoDic setValue:textViewText forKey:[self.typeDic objectForKey:self.clickCell.typeName.text]];
            }];
            signatureVC.originalSignature = self.clickCell.typeContent.text;
            [self.navigationController pushViewController:signatureVC animated:YES];
        }
    }else if(1 == indexPath.section){
        if (0 == indexPath.row) {
            // 性别
            if (self.clickCell.typeContentHiden.text.length >= 1) {
            }else{
                self.uploadInfoDic = nil;
                [self addSexActionSheet];
            }
        }else if (1 == indexPath.row) {
            // 出生年月
            self.uploadInfoDic = nil;
//            [self addPickerViewShowTime];
            
            [self.view bringSubviewToFront:self.myDatePickerView];
            //点击“出生年月”让年月选择器上来
            [UIView animateWithDuration:0.35 animations:^{
                
                self.myDatePickerView.frame = CGRectMake(0, kScreenHeight-iOS7TopMargin-200, kScreenWidth, 200);
            } completion:^(BOOL finished) {
                
            }];
            
        }else if (2 == indexPath.row){
            self.uploadInfoDic = nil;
            // 地区
//            [self addLocatePicker];
            [UIView animateWithDuration:0.35 animations:^{
                self.locatePicker.frame = CGRectMake(0, kScreenHeight-iOS7TopMargin-200, kScreenWidth, 200);
            } completion:^(BOOL finished) {
                
            }];
            
            
        }else if (3 == indexPath.row){
            // 身份证号
            if (self.userDetailObject.idcard.length > 0) {
                
            }else{
                 [self didSelectTableViewRowJumpAction:self.nameTypeArray[2] placeholderStr:self.nextViewPlaceholder[2]];
            }
        }
        else if (4 == indexPath.row){
            // 个人邮箱
            if (self.userDetailObject.email.length > 1) {
            }else{
                [self didSelectTableViewRowJumpAction:self.nameTypeArray[3] placeholderStr:self.nextViewPlaceholder[3]];
            }
        }
        
       /*
        else if (5 == indexPath.row){
            PersonalMobileViewController *personalMobileVC = [[PersonalMobileViewController alloc] init];
            // 从PersonalMobileViewController界面传过来的电话号码数据
            [personalMobileVC getTextFieldInfo:^(NSString *textFieldStr) {
                self.clickCell.typeContent.text = textFieldStr;
            }];
            [self.navigationController pushViewController:personalMobileVC animated:YES];
        }
    */
    }
}


#pragma mark 新的生日picker
-(void)makeBirthdayPickerView
{
    self.myDatePickerView = [[MyDatePickerView alloc] initWithFrame:CGRectMake(0, kScreenHeight-iOS7TopMargin, kScreenWidth, 200)];
    [self.view addSubview:self.myDatePickerView];
    
    [self.myDatePickerView.cancleButton addTarget:self action:@selector(birthDateButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    self.myDatePickerView.cancleButton.titleLabel.font = [UIFont systemFontOfSize:18];
    self.myDatePickerView.cancleButton.tag = 1000;
    [self.myDatePickerView.sureButton addTarget:self action:@selector(birthDateButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    self.myDatePickerView.sureButton.titleLabel.font = [UIFont systemFontOfSize:18];
    self.myDatePickerView.sureButton.tag = 1001;
    
}
/**
 *  取消  确定  按钮触发的事件
 *
 *  @param button 按钮的tag值1000是“取消”，1001是“确定”
 */
-(void)birthDateButtonClicked:(UIButton*)button
{
    switch (button.tag) {
        case 1000:
        {
            [UIView animateWithDuration:0.35 animations:^{
                self.myDatePickerView.frame = CGRectMake(0, kScreenHeight-iOS7TopMargin, kScreenWidth, 200);
            } completion:^(BOOL finished) {
            }];
        }
            break;
        case 1001:
        {
            self.clickCell.typeContent.text = [self.myDatePickerView getDateString];
            [self.uploadInfoDic setValue:[self.myDatePickerView getDateString] forKey:[self.typeDic objectForKey:self.clickCell.typeName.text]];
            [self _parsePersonalDataSource:kBaseURL];
            {
                [UIView animateWithDuration:0.35 animations:^{
                    self.myDatePickerView.frame = CGRectMake(0, kScreenHeight-iOS7TopMargin, kScreenWidth, 200);
                } completion:^(BOOL finished) {
                    
                }];
            }
        }
            break;
            
        default:
            break;
    }
    
}


#pragma mark - 封装的一个方法，当点击cell的做的内部操作 -
/**
 *  封装的一个方法，当点击cell的做的内部操作
 *
 *  @param title       下一个页面的naviBar title
 *  @param placeholder 下一个页面的textField的placeholder
 */
- (void)didSelectTableViewRowJumpAction:(NSString *)title placeholderStr:(NSString *)placeholder{
     ModifyPersonInfoViewController *modifyVC = [[ModifyPersonInfoViewController alloc] init];
    modifyVC.tabTitle = title;
    modifyVC.placehoderString = placeholder;
    [modifyVC getTextFieldInfo:^(NSString *textFieldStr) {
        self.clickCell.typeContent.text = textFieldStr;
        // 存入要上传的字典中(不需要了 这几个在跳转到下个页面直接上传了)
//        [self.uploadInfoDic setValue:textFieldStr forKey:[self.typeDic objectForKey:self.clickCell.typeName.text]];
    }];
    [self.navigationController pushViewController:modifyVC animated:YES];
}


#pragma mark - 实现点击实现性别的选择 -
- (void)addSexActionSheet{
    if (!sexBool) {
        UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"选取性别" delegate: self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"男",@"女", nil];
        [sheet showInView: [[UIApplication sharedApplication].delegate window]];
    }else{
        // 提示不可修改
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示:" message:@"对不起不能再次修改" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        [alertView show];
    }
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex != 2) {
        self.actionSheetButtonIndex = buttonIndex;
        UIAlertView *sexAlertView = [[UIAlertView alloc] initWithTitle:@"性别的选取" message:@"确定后不可修改" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定",@"取消", nil];
        [sexAlertView show];
    }
}

- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    switch (buttonIndex) {
        case 0:
            if (self.actionSheetButtonIndex == 0) {
                if (!sexBool) {
                    self.clickCell.typeContentHiden.text = @"男";
                    self.clickCell.typeContent.hidden = YES;
                    self.clickCell.typeContentHiden.hidden = NO;
                    self.clickCell.jumpImageView.hidden = YES;
                    [self.uploadInfoDic setValue:@"1" forKey:[self.typeDic objectForKey:self.clickCell.typeName.text]];
                    [self _parsePersonalDataSource:kBaseURL];
                }
            }else if (self.actionSheetButtonIndex == 1){
                if (!sexBool) {
                    self.clickCell.typeContentHiden.text = @"女";
                    self.clickCell.typeContent.hidden = YES;
                    self.clickCell.typeContentHiden.hidden = NO;
                    self.clickCell.jumpImageView.hidden = YES;
                    [self.uploadInfoDic setValue:@"2" forKey:[self.typeDic objectForKey:self.clickCell.typeName.text]];
                    [self _parsePersonalDataSource:kBaseURL];
                }
            }
            [self.settings setBool:YES forKey:@"Bool"];
            sexBool = [self.settings boolForKey:@"Bool"];
            break;
        case 1:
            break;
        default:
            break;
    }
}

#pragma mark - 获取当前的时间 -
- (NSString *)setNewTime{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"YYYY-MM-dd"];
    NSString *locationString=[formatter stringFromDate: [NSDate date]];
    return locationString;
}

#pragma mark - (旧的)实现点击出生年月时执行的pickerView -
//- (void)addPickerViewShowTime{
//   __block NSString *string = self.clickCell.typeContent.text;
//    if (nil != self.xinsView) {
//        [self.xinsView removeFromSuperview];
//    }
//    self.xinsView = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 200, self.view.frame.size.width, 200)];
//    [self.view addSubview:self.xinsView];
//    self.datePicker = [[HYDatePicker alloc]initWithDate:nil type:HYDatePickerTypePlain showInView:self.xinsView handler:^(HYDatePicker *picker, BOOL finished) {
//        
//        if (0 == [picker getDateString].length) {
//            self.clickCell.typeContent.text = string;
//        }else{
//            self.clickCell.typeContent.text = [picker getDateString];
//            [self.uploadInfoDic setValue:[picker getDateString] forKey:[self.typeDic objectForKey:self.clickCell.typeName.text]];
//            [self _parsePersonalDataSource:kBaseURL];
//        }
//        [self.xinsView removeFromSuperview];
//    }];
//    [self.datePicker show];
//}

#pragma mark - picker实现地区选择 -
- (void)addLocatePicker{
    self.locatePicker = [[HZAreaPickerView alloc] initWithStyle:HZAreaPickerWithStateAndCityAndDistrict delegate:self];
    self.locatePicker.frame = CGRectMake(0, kScreenHeight, kScreenWidth, 200);
    
    self.locatePicker.delegate = self;
    [self.view addSubview:self.locatePicker];
    [self.locatePicker.cancelBtn addTarget:self action:@selector(locatePickerCancelBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.locatePicker.okBtn addTarget:self action:@selector(locatePickerOkBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    self.locatePicker.okBtn.frame = CGRectMake(kScreenWidth - 55, 15, 40, 20);
    [[UIApplication sharedApplication].keyWindow endEditing:YES];
}

// 地区picker取消按钮的实现
-(void)locatePickerCancelBtnClicked:(UIButton *)button{
    [UIView animateWithDuration:0.35 animations:^{
        self.locatePicker.frame = CGRectMake(0, kScreenHeight-iOS7TopMargin, kScreenWidth, 200);
    } completion:^(BOOL finished) {
        
    }];
}
// 地区picker确定按钮的实现
-(void)locatePickerOkBtnClicked:(UIButton *)button{
    [UIView animateWithDuration:0.35 animations:^{
        self.locatePicker.frame = CGRectMake(0, kScreenHeight-iOS7TopMargin, kScreenWidth, 200);
    } completion:^(BOOL finished) {
        
    }];
    if ([self.areaString length] == 0) {
        self.areaString = @"北京  西城";
        [self.uploadInfoDic setObject:@"1" forKey:@"provinceid"];
        [self.uploadInfoDic setObject:@"102" forKey:@"cityid"];
    }
    self.clickCell.typeContent.text = self.areaString;
    // 存储到字典中
    [self.uploadInfoDic setValue:self.areaString forKey:[self.typeDic objectForKey:self.clickCell.typeName.text]];
    change = YES;
    // 上传
    [self _parsePersonalDataSource:kBaseURL];
}


#pragma mark - HZAreaPicker delegate -
- (void)pickerDidChangeFirst:(HZLocation *)picker1 andSecond:(HZLocation *)picker2{
//    change = NO;
    if (picker2.cityName.length == 0) {
        self.areaString = [NSString stringWithFormat:@"%@", picker1.cityName];
    }else{
        self.areaString = [NSString stringWithFormat:@"%@ %@", picker1.cityName, picker2.cityName];
        self.cityId = [NSString stringWithFormat:@"%ld",(long)picker2.cityID];
        [self.uploadInfoDic setObject:self.cityId forKey:@"cityid"];
    }
    
    self.proviceId = [NSString stringWithFormat:@"%ld",(long)picker1.cityID];
    
    [self.uploadInfoDic setObject:self.proviceId forKey:@"provinceid"];
    
    
}



#pragma mark - 网路数据上传 -
- (void)_parsePersonalDataSource:(NSString *)urlString{
    // 获取url地址
    NSString * postURL = [NSString stringWithFormat:@"%@user/changeuser/",urlString];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [self.uploadInfoDic setValue:kPlatform forKey:@"platform"];
    [self.uploadInfoDic setValue:kVersion forKey:@"version"];
    MyNSLog("%@",self.uploadInfoDic);
    
    NSLog(@"********%@",self.uploadInfoDic);
    [manager POST:postURL parameters:self.uploadInfoDic success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSNumber * statusNum = [responseObject objectForKey:@"status"];
        NSNumberFormatter* numberFormatter = [[NSNumberFormatter alloc] init];
        NSString * statusString = [numberFormatter stringFromNumber:statusNum];
        
        if ([statusString isEqualToString:@"0"]) {
            alertContent([responseObject objectForKey:@"msg"]);
        }else{
            MyNSLog("======%@========", [responseObject objectForKey:@"msg"]);
            MyNSLog("上传的字典 = %@", self.uploadInfoDic);
            UserModel *userModel = [[UserManager shareInstance]getUserInfo];
            NSUserDefaults *userdefault=[NSUserDefaults standardUserDefaults];
            NSString *currentAge;
            
            if ([[self.uploadInfoDic objectForKey:@"gender"] length]>0) {
                [userdefault setObject:[self.uploadInfoDic valueForKey:@"gender"] forKey:[NSString stringWithFormat:@"%@gender",userModel.uid]];
            }
            else if ([[self.uploadInfoDic valueForKey:@"birthday"] length]>0) {
                currentAge=[self calculateAge:[self.uploadInfoDic valueForKey:@"birthday"]];
                [userdefault setObject:currentAge forKey:[NSString stringWithFormat:@"%@age",userModel.uid]];
            }
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//        NSLog(@"      %@",operation.responseString);
    }];

}


#pragma mark - 网路数据请求 -
- (void)refreshData:(NSString *)urlString{
    // 获取url地址
    NSString * postURL = [NSString stringWithFormat:@"%@user/",urlString];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSDictionary *parameters = @{@"platform": kPlatform,@"version":kVersion};
    [manager POST:postURL parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSNumber * statusNum = [responseObject objectForKey:@"status"];
        NSNumberFormatter* numberFormatter = [[NSNumberFormatter alloc] init];
        NSString * statusString = [numberFormatter stringFromNumber:statusNum];
        if ([statusString isEqualToString:@"0"]) {
            alertContent([responseObject objectForKey:@"msg"]);
        }else{
            NSDictionary * dataDic = [responseObject objectForKey:@"data"];
            [self.userDetailObject setValuesForKeysWithDictionary:dataDic];
        }
        [self.editPersonalDataTableView reloadData];
    } failure:nil];
}
/**
 *  出生年月，计算实际年龄
 */
- (NSString *)calculateAge:(NSString *)age
{
    NSArray *array=[age componentsSeparatedByString:@"-"];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSString *strDate = [dateFormatter stringFromDate:[NSDate date]];
    NSArray *currentArray=[strDate componentsSeparatedByString:@"-"];
    NSInteger agee=0;
    NSInteger year=[[currentArray objectAtIndex:0] intValue]-[[array objectAtIndex:0] intValue];
    NSInteger month=[[currentArray objectAtIndex:1] intValue]-[[array objectAtIndex:1] intValue];
    NSInteger day=[[currentArray objectAtIndex:2] intValue]-[[array objectAtIndex:2] intValue];
    if (year>0) {
        agee=year-1;
    }
    if (month>0) {
        agee=agee+1;
    }else if (month==0)
    {
        if (day>=0) {
            agee=agee+1;
        }
    }
    return [NSString stringWithFormat:@"%ld",(long)agee];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
